package org.itboys.weixin;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.fuzion.dto.FuzionMemberRedisDto;
import org.itboys.fuzion.service.FuzionMemberService;
import org.itboys.manager.ProductWeixinService;
import org.itboys.product.entity.Product;
import org.itboys.product.entity.ProductCategory;
import org.itboys.product.entity.ProductText;
import org.itboys.product.service.ProductService;
import org.itboys.product.service.ProductTextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by huml on 2016/4/15.
 * 微信产品控制层
 */
@RestController
@RequestMapping("/weixin/product")
public class ProductWeixinController extends BaseController {
    @Autowired
    private ProductWeixinService productWeixinService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTextService productTextService;
	@Autowired
	private FuzionMemberService fuzionMemberService;

    /**
     * 商城首页
     * @param request
     * @param model
     */
    @RequestMapping("/productIndex")
    public ModelAndView productIndex(HttpServletRequest request,
                                     @Param("memberId")Long memberId, HttpServletResponse response, Model model){
//    	Long memberId = (Long) request.getSession().getAttribute("memberId");
////        memberId = 69L;
//    	if(memberId == null){
//    		return new ModelAndView("error");
//    	}
//    	FuzionMemberRedisDto fmrd = fuzionMemberService.getFuzionMemberRedisDto(memberId);
//    	if(fmrd == null){
//    		return new ModelAndView("error");
//    	}
//        Long sellerId = fmrd.getSellerId();
//        if(sellerId == null){
//            sellerId = 2L;
//        }
//        List<ProductCategory> categories = productWeixinService.productIndexData(sellerId);
//        model.addAttribute("data", categories);
        if(memberId != null){
            fuzionMemberService.setSessionMember(memberId, request, response);
        }
        return new ModelAndView("site/productIndex1");
    }

    /**
     * 能量商城首页
     * @param request
     * @param model
     */
    @RequestMapping("/beanProductIndex")
    public ModelAndView beanProductIndex(HttpServletRequest request, Model model){
        Long memberId = (Long) request.getSession().getAttribute("memberId");
//        memberId = 69L;
        if(memberId == null){
            return new ModelAndView("error");
        }
        FuzionMemberRedisDto fmrd = fuzionMemberService.getFuzionMemberRedisDto(memberId);
        if(fmrd == null){
            return new ModelAndView("error");
        }
        Long sellerId = fmrd.getSellerId();
        if(sellerId == null){
            sellerId = 2L;
        }
        List<ProductCategory> categories = productWeixinService.productIndexData(sellerId);
        model.addAttribute("data", categories);
        return new ModelAndView("site/productIndex");
    }

    /**
     * 产品详情页
     * @param productId
     * @param model
     */
    @RequestMapping("/productDetail")
    public ModelAndView productDetail(@RequestParam("productId")Long productId,
                              Model model){
        Product product = productService.getById(productId);
        BigDecimal price = new BigDecimal(product.getPrice());
        BigDecimal proPrice = new BigDecimal(product.getProPrice());
        proPrice = proPrice.divide(new BigDecimal(100));
        price = price.divide(new BigDecimal(100));
        product.setPrice(price.intValue());
        product.setProPrice(proPrice.intValue());
        ProductText productText = productTextService.getByField("productId", productId);
        //Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
        model.addAttribute("product", product);
        model.addAttribute("productText", productText);
        return new ModelAndView("site/productDetail");
    }

}
